﻿using System.ComponentModel.Composition;
using System.Web.Services.Description;
using Laps.Lib.Services;
using System;
using System.Xml;
using System.Xml.Schema;

namespace Laps.Lib.WSDL.Services
{
    [Export(typeof(Laps.Lib.Services.IService))]
    public class Service : IService
    {
        #region Méthodes
        public Service()
        {
            this.Name = "WSDL";
            this.Namespace = "";
            this.Presents = null;
        }
        #endregion

        #region Propriétés
        public string Name
        {
            get;
            private set;
        }
        public string Namespace
        {
            get;
            private set;
        }
        public void Open(string filename)
        {
            ServiceDescription sdDescription = ServiceDescription.Read(filename);

            foreach (Binding btest in sdDescription.Bindings)
            {
                
                Console.WriteLine(string.Format("{0} : Binding", btest.Name));
                foreach (OperationBinding opTemp in btest.Operations)
                {                                      
                    Console.WriteLine(string.Format("   {0} : OperationBinding",opTemp.Name));
                }

            }
            Console.WriteLine();
            foreach (System.Web.Services.Description.Service stemp in sdDescription.Services)
            {
                Console.WriteLine("{0} : Service",stemp.Name);
                
                foreach (Port ptemp in stemp.Ports)
                {
                    Console.WriteLine(" {0} : Port",ptemp.Name);
                }
            }
            Console.WriteLine();
            foreach (PortType ptTemp in sdDescription.PortTypes)
            {
                foreach (Operation opTemp in ptTemp.Operations)
                {
                    Console.WriteLine("{0} : Operation",opTemp.Name);
                }
            }
            Console.WriteLine();
            foreach (Message mtest in sdDescription.Messages)
            {
                Console.WriteLine("{0} : Message",mtest.Name);
                foreach (MessagePart mpTemp in mtest.Parts)
                {
                    Console.WriteLine("        {0} : Part", mpTemp.Name);
                    Console.WriteLine("         {0} : Element", mpTemp.Element.Namespace);                            
                }
            }
            Console.WriteLine();   
            foreach (XmlSchema xsTemp in sdDescription.Types.Schemas)
            {
                Console.WriteLine("{0} : XmlSchema", xsTemp.TargetNamespace);
                Console.WriteLine("{0} : Attributs", xsTemp.Attributes.Count);
                Console.WriteLine("{0} : Elements", xsTemp.Elements.Count);
                foreach (var test in xsTemp.Namespaces.ToArray())
                {
                    Console.WriteLine(test.Name);
                }
                foreach (var test in xsTemp.Elements.Names)
                {
                    Console.WriteLine(test);
                }
            }
        }
        public ServiceProfile Presents
        {
            get;
            set;
        }
        #endregion
    }
}
